package com.fbli.test.service.impl;

import com.fbli.test.entity.Authority;
import com.fbli.test.entity.User;
import com.fbli.test.mapper.AuthorityMapper;
import com.fbli.test.service.AuthorityService;
import com.fbli.test.service.UserService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AuthorityServiceImpl implements AuthorityService {
    @Autowired
    AuthorityMapper authorityMapper;
    @Autowired
    UserService userService;
    @Override
    public Authority selectByPrimaryKey(Integer id) {
        Authority authority = authorityMapper.selectByPrimaryKey(id);
        return authority;
    }

    @Override
    public List<Authority> selectByUserId(long userId) {
        return authorityMapper.selectByUserId(userId);
    }

    @Override
    public List<Authority> selectAll() {
        return authorityMapper.selectAll();
    }

    @Override
    public boolean addAuthority(String[] prems, long userId,String role) {
        User user = userService.selectByPrimaryKey(userId);
        if(user==null){
            return false;
        }else {
            Authority authority = new Authority();
            for (String prem:prems){
                authority.setUserId(userId);
                authority.setPerms(prem);
                authority.setRole(role);
                authorityMapper.insert(authority);
            }
            return true;
        }

    }
}
